#include <stdio.h>
#include <time.h>
#include <malloc.h>
#include "Course.h"
#include "../ModuleC/Log.h"
#include "../ModuleA/Student.h"
#include "../include/Course_pub.h"

void PrintChoice3() {
	printf("*****************************************\n");
	printf("*           课程信息管理            	*\n");
	printf("*****************************************\n");
	printf("1. 添加或录入课程信息\n");
	printf("2. 删除课程信息\n");
	printf("3. 按课程编号修改课程信息\n");
	printf("4. 查询全部课程信息\n");
	printf("5. 按课程编号查询学生信息\n");
	printf("6. 退出\n");
	printf("------------------------------------------\n");
	printf("请做出一个选择！\n");
}

void CourseManage() {//课程管理界面
	int num;
	while (1) {
	  	PrintChoice3();
	  	scanf("%d", &num);
	  	switch (num) {
	  	case 1:
	  		AddCImformation();
	  		break;
	  	case 2:
	  		DelCImformation();
	  		break;
	  	case 3:
	  		AlterCImformation();
	  		break;
		case 4:
	  		QueryCImformation1();
	  		break;
		case 5:
	  		QueryCImformation2();
	  		break;  	
	  	case 6:
	  		break;
	  	default:
	  		printf("请输入一个1~6之间的数字！\n"); 
			break;
	  	}
	  	if (num == 6) {
			break;
		}
	}
}
void AddCImformation(){//添加课程信息
	int num;
	while (1) {
		printf("*****************************************\n");
		printf("*             添加课程信息         	*\n");
		printf("*****************************************\n");
		printf("1.录入课程信息\n");
		printf("2.新增课程信息\n");
		printf("3.退出\n");
		printf("------------------------------------------\n");
		printf("请做出一个选择！\n");
		scanf("%d",&num);
		switch (num) {
		case 1:
			ReadCImformation();
			break;
		case 2:
			AddCImformationByK();
			break;
		case 3:
			break;
		default: 
			printf("请输入一个1~3之间的数字！\n"); 
			break;
		}
        	if (num == 3) {
			break;
		}
	}
}
void AlterCImformation(){//修改课程信息
	int i;
	if (headC == NULL) {
		printf("课程信息还未录入，请先录入课程信息！\n");
		return ;
	}
	char s[15];
	struct Course *p1;
	p1 = headC;
	printf("请输入你要修改的课程的课程号或课程名！");
	scanf("%s", s); 
	while (strcmp(s, p1->CNO) && strcmp(s, p1->CN) && p1->next) {
		p1 = p1->next;
	}
	if (!strcmp(s, p1->CNO) || !strcmp(s, p1->CN)) {
		printf("请输入你要修改的课程信息！\n"); 
		printf("请输入课程编号，课程名，课程性质！\n");
		scanf("%s %s %s", p1->CNO, p1->CN, p1->CP);
		printf("请输入授课学时，学分，课程已选人数，课程人数上限！\n");
		scanf("%lf %lf %d %d", &p1->CT, &p1->CS, &p1->ASN, &p1->LSN); 
		printf("请输入已选课程学生名：\n");
		for(i = 0; i < p1->ASN; i++)
			scanf("%s", p1->CSN[i]);
		printf("修改课程信息成功！\n");
	} else {
		printf("课程信息不存在，请重新输入！");
		return ; 
	}
	FILE *fp1;	
	fp1=fopen("FILE/log.log","a");
	Log(fp1);
	fprintf(fp1,"修改了%s的课程信息，调用了AlterCImformation()。\n", s);
	fclose(fp1);
}
void DelCImformation(){//删除课程信息
	if (headC == NULL) {
		printf("课程信息还未录入，请先录入课程信息！\n");
		return ;
	}
	char s[15];
	struct Course *p1, *p2;
	p1 = headC;
	printf("请输入你要删除的课程的课程号或课程名！");
	scanf("%s", s); 
	while (strcmp(s, p1->CNO) && strcmp(s, p1->CN) && p1->next) {
		p2 = p1;
		p1 = p1->next;
	}
	if (!strcmp(s, p1->CNO) || !strcmp(s, p1->CN)) {
		if (p1 == headC) {
			headC = p1->next;
		} else {
			p2->next = p1->next;
		}
		printf("删除课程信息成功！\n");	
	} else {
		printf("课程信息不存在，请重新输入！");
		return ; 
	}
	FILE *fp1;	
	fp1=fopen("FILE/log.log","a");
	Log(fp1);
	fprintf(fp1,"删除了课程编号为%s，课程名为%s的课程信息，调用了DelCImformation()。\n",p1->CNO, p1->CN);
	fclose(fp1);
}
void QueryCImformation1(){//查询全部课程信息
	if (headC == NULL) {
		printf("课程信息还未录入，请先录入课程信息！\n");
		return ;
	}
	int i;
	struct Course *p1;
	p1 = headC;
	printf("课程号\t课程名称  属性\t学时\t学分\t已选\t上限\t已选学生\n");
	while (p1) {
		printf("%s\t%s  %s\t", p1->CNO, p1->CN, p1->CP);
		printf("%.0lf\t%.0lf\t%d\t%d\t", p1->CT, p1->CS, p1->ASN, p1->LSN);
		for(i = 0; i < p1->ASN; i++)
			printf("%s\t", p1->CSN[i]);
		printf("\n");
		if (p1->next) {
			p1 = p1->next;	
		} else {
			break;
		}
	}
	FILE *fp1;	
	fp1=fopen("FILE/log.log","a");
	Log(fp1);
	fprintf(fp1,"查询了所有的课程信息，调用了QueryCImformation1()。\n");
	fclose(fp1);
}
void QueryCImformation2(){//查询单个课程信息
	if (headC == NULL) {
		printf("课程信息还未录入，请先录入课程信息！\n");
		return ;
	}
	int i, flag = 1;
	char s[15];
	struct Course *p1;
	printf("请输入你要查找的课程的课程号或课程名！");
	scanf("%s", s); 
	p1 = headC;
	while (p1) {
		if (!strcmp(s, p1->CNO) || !strcmp(s, p1->CN)) {
			flag = 0;
			printf("课程号\t课程名称  属性\t学时\t学分\t已选\t上限\t已选学生\n");
			printf("%s\t%s  %s\t", p1->CNO, p1->CN, p1->CP);
			printf("%.0lf\t%.0lf\t%d\t%d\t", p1->CT, p1->CS, p1->ASN, p1->LSN);
			for(i = 0; i < p1->ASN; i++)
				printf("%s\t", p1->CSN[i]);
			printf("\n");
		}
		if (p1->next) {
			p1 = p1->next;	
		} else {
			break;
		}
	}
	if (flag)
		printf("未查找到该课程信息\n");
	FILE *fp1;	
	fp1=fopen("FILE/log.log","a");
	Log(fp1);
	fprintf(fp1,"查询了课程编号为%s，课程名为%s的课程信息，调用了QueryCImformation2()。\n",p1->CNO, p1->CN);
	fclose(fp1);
}

void AddCImformationByK(){//添加课程信息
	if (headC == NULL) {
		printf("课程信息还未录入，请先录入课程信息！\n");//new
		return ;
	}
	int i;
	struct Course *p1, *p2;
	p1 = (struct Course *)malloc(sizeof(struct Course));
	printf("请输入课程编号，课程名，课程性质！\n");
	scanf("%s %s %s", p1->CNO, p1->CN, p1->CP);
	p2 = headC;
	while (p2) {
		if (!strcmp(p1->CNO,p2->CNO)) {
			printf("该课程信息已存在，请重新输入！\n");//new
			return ;
		}
		p2 = p2->next;
	}
	printf("请输入授课学时，学分，课程已选人数，课程人数上限！\n");
	scanf("%lf %lf %d %d", &p1->CT, &p1->CS, &p1->ASN, &p1->LSN); 
	printf("请输入已选课程学生名：\n");
	for(i = 0; i < p1->ASN; i++)
		scanf("%s", p1->CSN[i]);
	p1->next = headC->next;
	headC->next = p1;
	printf("添加课程信息成功！\n");
	FILE *fp1;	
	fp1=fopen("FILE/log.log","a");
	Log(fp1);
	fprintf(fp1,"新增了课程编号为%s，课程名为%s的课程信息，调用了AddCImformationByK()。\n",p1->CNO, p1->CN);
	fclose(fp1);
}

void PreserveC() {//保存课程信息
	if (headC == NULL) {
		printf("课程信息还未录入，请先录入课程信息！\n");
		return ;
	}
	int i;
	FILE *fp;
	char filename[20];
	struct Course *p1;
	printf("请输入保存文件路径及文件名：\n");
	scanf("%s", filename);
	if ((fp = fopen(filename, "w+")) == NULL) {
        	printf("打开文件失败！\n"); 
		return ; 
    	}
    	p1 = headC;
    	while (p1) {
    		fprintf(fp, "%s %s %s", p1->CNO, p1->CN, p1->CP);
	    	fprintf(fp, " %.0lf %.0lf %d %d", p1->CT, p1->CS, p1->ASN, p1->LSN);
    		for(i = 0; i < p1->ASN; i++)
			fprintf(fp, " %s", p1->CSN[i]);
		if(p1->next)
			fprintf(fp, "\n");
    		p1 = p1->next;
    	}
	free(p1);
	free(headC);
	fclose(fp);
	printf("课程信息已保存在%s中!\n",filename);
	FILE *fp1;	
	fp1=fopen("FILE/log.log","a");
	Log(fp1);
	fprintf(fp1,"保存了课程信息，调用了PreserveC()。\n");
	fclose(fp1);
}
